package cn.xorange.commons.system.controller.platform;


import cn.hutool.core.util.ReflectUtil;
import cn.xorange.commons.core.basic.bean.PageBean;
import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.controller.DataController;
import cn.xorange.commons.data.core.entity.TreeEntity;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.security.annotation.PassToken;
import cn.xorange.commons.system.entity.platform.SysAgreement;
import cn.xorange.commons.system.service.platform.ISysAgreementService;
import cn.xorange.commons.system.service.platform.ISysProductSpuService;
import cn.xorange.commons.utils.async.AsyncManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @author : yangjian
 * date : 2022/2/2
 * description : 协议
 */
@RestController
@Authorize(hasPerm = "platform:agreement")
@RequestMapping("/platform/agreement")
public class SysAgreementController extends DataController<SysAgreement, ISysAgreementService> {

    @Resource
    ISysProductSpuService spuService;

    @Override
    @Authorize(hasPerm = "list")
    @GetMapping("/list")
    public PageBean list(SysAgreement agreement){
        super.startPage();
        agreement.setExcludeColumn("content");
        List<SysAgreement> list = this.service.getList(agreement);
        return new PageBean(list);
    }

    @PassToken
    @PutMapping("/agreementLog")
    public ResultBean InsertAgreementLog(@RequestBody Long[] ids){
        AsyncManager.me().execute(service.insertSysAgreementLog(ids));
        return ResultBean.onSuccess();
    }

    @Override
    @Authorize(hasPerm = "remove'")
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean del(@PathVariable Long id) {
        int num = spuService.checkSpuByAgreementId(id);
        if(num>0){
            return ResultBean.onFail("协议被认证模块使用，不可删除");
        }

        this.service.deleteById(id);
        return ResultBean.onSuccess();
    }

}